#include "bsp_adc.h"
#include <stdio.h>
#include "adc.h"
#include "global_VariableDef.h"
 
/**
 * @brief: 触发一次ADC转换
 * @param: adc_handle -- ADC对象
 * @return: void
 */
void bsp_adc_DataProc(ADC_HandleTypeDef *adc_handle)
{
    // 相电流消零
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesA = g_invertorParam.adc_raw_values[3] - g_invertorParam.motor_phaseCurrent.offset_phaseA;
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesB = g_invertorParam.adc_raw_values[4] - g_invertorParam.motor_phaseCurrent.offset_phaseB;
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesC = g_invertorParam.adc_raw_values[5] - g_invertorParam.motor_phaseCurrent.offset_phaseC;

    g_invertorParam.motor_phaseCurrent.adc_phase_valuesA = g_invertorParam.motor_phaseCurrent.offset_phaseA - g_invertorParam.adc_raw_values[0];
    g_invertorParam.motor_phaseCurrent.adc_phase_valuesB = g_invertorParam.motor_phaseCurrent.offset_phaseB - g_invertorParam.adc_raw_values[1];
    g_invertorParam.motor_phaseCurrent.adc_phase_valuesC = g_invertorParam.motor_phaseCurrent.offset_phaseC - g_invertorParam.adc_raw_values[2]; 
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesA = g_invertorParam.adc_raw_values[3] - g_invertorParam.motor_phaseCurrent.offset_phaseA;
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesB = g_invertorParam.adc_raw_values[4] - g_invertorParam.motor_phaseCurrent.offset_phaseB;
    // g_invertorParam.motor_phaseCurrent.adc_phase_valuesC = g_invertorParam.adc_raw_values[5] - g_invertorParam.motor_phaseCurrent.offset_phaseC;
}

// 内部函数：将原始ADC值转换为电压
void bsp_adc_VbusCalculate(void)
{
    // 0.0412 是母线分压比例, 2.5 是零偏值(没有实际测量过)
    g_invertorParam.motor_vBus.VBus = (g_invertorParam.adc_raw_values[7] * 3.3f / 4095.0f) / 0.0412f - 2.5f;


    g_invertorParam.motor_phaseCurrent.current_PhaseA = (g_invertorParam.motor_phaseCurrent.adc_phase_valuesA * 3.0f / 4096.0f) / 20.0f / 0.001f;
    g_invertorParam.motor_phaseCurrent.current_PhaseB = (g_invertorParam.motor_phaseCurrent.adc_phase_valuesB * 3.0f / 4096.0f) / 20.0f / 0.001f;
    g_invertorParam.motor_phaseCurrent.current_PhaseC = (g_invertorParam.motor_phaseCurrent.adc_phase_valuesC * 3.0f / 4096.0f) / 20.0f / 0.001f;
    // g_invertorParam.motor_phaseCurrent.current_PhaseA = - (g_invertorParam.motor_phaseCurrent.current_PhaseB + g_invertorParam.motor_phaseCurrent.current_PhaseC);
}

/**
 * @brief: ADC 上电检测零偏
 */
void bsp_adc_CalibrateZeroOffset(void)
{
    static uint32_t sumA = 0, sumB = 0, sumC = 0;
    static uint32_t calibrate_times = 0;

    calibrate_times ++;
    if ((calibrate_times < CALIBRATION_SAMPLES))
    {
        sumA += g_invertorParam.adc_raw_values[0];  // 读取A相
        sumB += g_invertorParam.adc_raw_values[1];  // 读取B相
        sumC += g_invertorParam.adc_raw_values[2];  // 读取C相
        
        g_invertorParam.motor_phaseCurrent.offset_phaseA = (int16_t)(sumA / calibrate_times);
        g_invertorParam.motor_phaseCurrent.offset_phaseB = (int16_t)(sumB / calibrate_times);
        g_invertorParam.motor_phaseCurrent.offset_phaseC = (int16_t)(sumC / calibrate_times);
    }
    else
    {
        g_invertorParam.adc_offset_flag = 1;        // 零偏检测完成
        calibrate_times = CALIBRATION_SAMPLES;
    }
}
